<?php

class Application_Model_ForumTopicMapper extends Ahs_Model_Mapper
{
    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Topics';
    }
    
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('topic_id = ?' => $id));
        return $this->_toTopic($row);
    }
    
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();

        return $this->_toTopics($rows);
    }
    public function addTopic($projectid, $title, $creationdate, $creator)

    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $count = $db->insert('topics', array('topic_project_id' => $projectid, 'topic_title' => $title, 'topic_creationdate' => $creationdate, 'topic_creator' => $creator));
        if($count > 0)
        {
            return true;
        }
        return false;
    }
    public function deleteTopic($topicid)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $affectedrows=$db->delete('topics', array('topic_id = ?' =>$topicid));
        if($affectedrows > 0)
        {
            return true;
        }
        return false;
    }
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Topic 
     */
    private function _toTopic($row = null)
    {
        $reply = new Application_Model_ForumTopic();

        if ($row) {
            $reply->Id              = $row['topic_id'];
            $reply->TopicProjectId  = $row['topic_project_id'];
            $reply->Title           = $row['topic_title'];
            $reply->Creationdate    = $row['topic_creationdate'];
            $reply->Creator         = $row['topic_creator'];
            $reply->Deleted         = $row['topic_deleted'];   
            $reply->Modifieddate    = $row['topic_modifieddate'];
        }
        return $reply;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toTopics($rows = null)
    {
        $replies = array();

        if ($rows) {
            foreach ($rows as $row) { 
                $replies[] = $this->_toReply($row);
            }
        }
        return $replies;
    } 
}